package com.cskaoyan.service;

import com.cskaoyan.bean.category.*;
import com.cskaoyan.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public BasePageData categoryList() {
        List<CategoryVo> categoryVoList = new ArrayList<>();
        //L1
        CategoryExample exampleL1 = new CategoryExample();
        CategoryExample.Criteria criteriaL1 = exampleL1.createCriteria();
        criteriaL1.andLevelEqualTo("L1");
        criteriaL1.andDeletedEqualTo(false);
        List<Category> categoriesL1 = categoryMapper.selectByExample(exampleL1);
        for (Category category : categoriesL1) {
            CategoryVo vo = new CategoryVo();
            vo.setId(category.getId());
            vo.setName(category.getName());
            vo.setKeywords(category.getKeywords());
            vo.setDesc(category.getDesc());
            vo.setIconUrl(category.getIconUrl());
            vo.setPicUrl(category.getPicUrl());
            vo.setLevel(category.getLevel());
            //L2
            CategoryExample exampleL2 = new CategoryExample();
            CategoryExample.Criteria criteriaL2 = exampleL2.createCriteria();
            criteriaL2.andLevelEqualTo("L2");
            criteriaL2.andDeletedEqualTo(false);
            criteriaL2.andPidEqualTo(category.getId());
            List<Category> categoriesL2 = categoryMapper.selectByExample(exampleL2);
            List<ChildrenDTO> childrenDTOS = new ArrayList<>();
            for (Category categL2 : categoriesL2) {
                ChildrenDTO child = new ChildrenDTO();
                child.setId(categL2.getId());
                child.setName(categL2.getName());
                child.setKeywords(categL2.getKeywords());
                child.setDesc(categL2.getDesc());
                child.setIconUrl(categL2.getIconUrl());
                child.setPicUrl(categL2.getPicUrl());
                child.setLevel(categL2.getLevel());

                childrenDTOS.add(child);

            }
            vo.setChildren(childrenDTOS);
            categoryVoList.add(vo);
        }


        return BasePageData.ok(categoryVoList);
    }

    @Override
    public BasePageData categoryL1() {
        List<CategoryL1> cL1s = new ArrayList<>();
        //L1
        CategoryExample exampleL1 = new CategoryExample();
        CategoryExample.Criteria criteriaL1 = exampleL1.createCriteria();
        criteriaL1.andLevelEqualTo("L1");
        List<Category> categoriesL1 = categoryMapper.selectByExample(exampleL1);
        for (Category category : categoriesL1) {

            CategoryL1 l1 = new CategoryL1();
            l1.setLabel(category.getName());
            l1.setValue(category.getId());
            cL1s.add(l1);
        }

        return BasePageData.ok(cL1s);
    }

    @Override
    public void updateCategory(Category category) {
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public void createCategory(Category category) {
        categoryMapper.insertSelective(category);
    }

    @Override
    public void deleteCategory(Category category) {
        //如果是父类目，那么查询子类目，把子类目deleted置为true

        if(categoryMapper.selectByPrimaryKey(category.getId()).getPid()==0){
            CategoryExample example = new CategoryExample();
            CategoryExample.Criteria criteria = example.createCriteria();
            criteria.andPidEqualTo(category.getId());
            List<Category> categories = categoryMapper.selectByExample(example);
            for (Category c : categories) {
                c.setDeleted(true);
                categoryMapper.updateByPrimaryKeySelective(c);
            }
        }
        category.setDeleted(true);
        categoryMapper.updateByPrimaryKeySelective(category);

    }
}
